package 地区数据;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class parseExcel {
    public static void main(String[] args) throws IOException {
        List<String> files_path = getAllFile("D:\\测试临时文件\\国家统计局-新增表格1-分省月度");

        for (String file_path : files_path){
            File file = new File(file_path);
            Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
            Sheet sheet = workbook.getSheetAt(0);
            int n = sheet.getLastRowNum()+1;
            for (int i = 1; i < n; i++) {
                String str = sheet.getRow(i).getCell(1).getStringCellValue();
                if (str.length() < 8){
                    String new_str = str.substring(0, 5) + "0" + str.substring(5);
                    sheet.getRow(i).createCell(1).setCellValue(new_str);
                }

            }
            workbook.write(new FileOutputStream(file_path));
            System.out.println(file_path + " IS UPDATE");
        }
    }

    public static List<String> getAllFile(String directoryPath) {
        //获取本地所有文件
        List<String> list = new ArrayList<>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                list.addAll(getAllFile(file.getAbsolutePath()));
            } else {
                list.add(file.getAbsolutePath());
            }
        }
        return list;
    }
}
